<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Catalog extends Controller_Index_Base
{
	public function action_index()
	{ 
        $category = $this->request->param('category') ? $this->request->param('category') : null;
        $brand    = $this->request->param('brand') ? $this->request->param('brand') : null;
        $color    = $this->request->param('color') ? $this->request->param('color') : null;
        $composition = $this->request->param('composition') ? $this->request->param('composition') : null;
        $season   = $this->request->param('season') ? $this->request->param('season') : null;
        $page     = $this->request->param('page') ? $this->request->param('page') : 1;
         
        $from_price = (isset($_GET['from_price']) && $_GET['from_price']) ? $_GET['from_price'] : null;
        $to_price   = (isset($_GET['to_price']) && $_GET['to_price']) ? $_GET['to_price'] : null;    
        $shop       = (isset($_GET['shop']) && $_GET['shop']) ? $_GET['shop'] : null;
        
        if ($category) {
            $category = ORM::factory('category')->get_category_by_url($category);   
        }  
        if ($brand) {
            $brand = ORM::factory('brand')->get_brand_by_url($brand);  
        } 
        $color    = Model_Color::get_color_by_url($color);
        $composition = Model_Composition::get_composition_by_url($composition);
        $season   = Model_Season::get_season_by_url($season);
        
        // TODO
        //if (Helper_Index::catalog_url($category) != '/catalog/category/'.$id.'-'.$url) {
        //    Helper_Index::redirect_301(Helper_Index::catalog_url($category, $brand, $color, $composition, $season, true));
        //}
        
        $categories   = ORM::factory('category')->get_categories($category, $brand, $color, $composition, $season, $shop, array('from' => $from_price, 'to' => $to_price));   
        $brands       = ORM::factory('brand')->get_brands($category, $brand, $color, $composition, $season, $shop, array('from' => $from_price, 'to' => $to_price));        
        //$colors       = Model_Color::get_colors($category, $brand, $color, $composition, $season, $shop, array('from' => $from_price, 'to' => $to_price));
        //$compositions = Model_Composition::get_compositions($category, $brand, $color, $composition, $season, $shop, array('from' => $from_price, 'to' => $to_price));
        //$seasons      = Model_Season::get_seasons($category, $brand, $color, $composition, $season, $shop, array('from' => $from_price, 'to' => $to_price));
        $prices       = Model_Product::get_prices($category, $brand, $color, $composition, $season, $shop);
        //$shops        = ORM::factory('shop')->get_shops($category, $brand, $color, $composition, $season, $shop, array('from' => $from_price, 'to' => $to_price));
        $products     = ORM::factory('product')->get_products($category, $brand, $color, $composition, $season, $shop, array('from' => $from_price, 'to' => $to_price), $page);
		$colors = $compositions = $seasons = $shops = array();
		 
        $content = new View('index/catalog/index_tpl'); 
        $content->category = $category;
        $content->brand    = $brand;
        $content->color    = $color;
        $content->composition = $composition;
        $content->season   = $season; 
        $content->breadcrumbs = null;
        if ($category) {
            $content->breadcrumbs = $category->get_breadcrumbs();   
        }
        $content->categories  = $categories;
        $content->brands   = $brands;
        $content->colors   = $colors;
        $content->compositions = $compositions;
        $content->seasons  = $seasons;
        $content->shops    = $shops;
        $content->prices   = $prices;
        $content->products = $products;
        $content->coupons  = ORM::factory('coupon')->get_coupons_by_products($products);
        $content->_shops   = ORM::factory('shop')->get_shops_by_products($products);
        $content->products_data = ORM::factory('product')->get_products_data($products);
        
        $count = ORM::factory('product')->get_products($category, $brand, $color, $composition, $season, $shop, array('from' => $from_price, 'to' => $to_price), $page, true);    
        $content->pagination = array(                                                   
            'page' => $page,
            'per_page' => Model_Product::get_products_per_page(),
            'count' => $count,
        );
        $content->filters = array(
            'from_price' => $from_price,
            'to_price'   => $to_price,
            'shop'       => $shop,
        ); 
        
        $content->text = null;
        if ($page == 1) {
            $content->text = Helper_Index::get_catalog_index_text($category, $brand, $color, $composition, $season);   
        }  
        
        $title       = Helper_Index::get_catalog_index_title($category, $brand, $color, $composition, $season, $page); 
        $description = Helper_Index::get_catalog_index_description($category, $brand, $color, $composition, $season, $page);        
        $this->print_template($title, $content, $description);
	}
    
    public function action_redir()
    {
        header('Location: /'); 
        die();  
    }
    
    /**
    * Чтобы редиректить на страницу с правильным урлом
    * 
    */
    /*public function action_old_index()
    { 
        $id       = $this->request->param('id') ? $this->request->param('id') : null;
        $brand_id = $this->request->param('brand_id') ? $this->request->param('brand_id') : null;
        $brand_url = $this->request->param('brand_url') ? $this->request->param('brand_url') : null;
        $color    = $this->request->param('color') ? $this->request->param('color') : null;
        $composition = $this->request->param('composition') ? $this->request->param('composition') : null;
        $season   = $this->request->param('season') ? $this->request->param('season') : null;
        $page     = $this->request->param('page') ? $this->request->param('page') : 1;
         
        $from_price = (isset($_GET['from_price']) && $_GET['from_price']) ? $_GET['from_price'] : null;
        $to_price   = (isset($_GET['to_price']) && $_GET['to_price']) ? $_GET['to_price'] : null;    
        $shop       = (isset($_GET['shop']) && $_GET['shop']) ? $_GET['shop'] : null;
        
        $category = ORM::factory('category')->filter($id); 
        $brand = null; 
        if ($brand_id) {
            $brand = ORM::factory('brand')->filter($brand_id);  
        } 
        Helper_Index::redirect_301(Helper_Index::catalog_url($category, $brand));
    } */
} 
